package com.youchuang.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.youchuang.bean.Product;
import com.youchuang.common.HibernateConnection;
import com.youchuang.dao.ProductDAO;

@Repository("ProductDAO")
@SuppressWarnings("rawtypes")
public class ProductDAOImpl implements ProductDAO {

	private static final String HQL_FIND_ALL_DESC = "from Product where hasDeleted = 0 ORDER BY id desc";
	private static final String HQL_FIND_BY_ID = "from Product where id = :id";
	private static final String HQL_FIND_BY_HASDELETE = "from Product where hasDeleted = :hasDeleted";
	private static final String HQL_FIND_BY_ADDTIME = "from Product where addTime = :addTime";
	private static final String HQL_FIND_BY_CATEGORYID = "from Product where categoryId = :categoryId";

	private static final String HQL_FIND_BY_CAT_ID = "from Product p join CategoryProduct c with p.id = c.productId where p.hasDeleted = 0 and c.categoryId = :categoryId ";
	private static final String HQL_FIND_BY_HOT = "FROM IndentItem i JOIN Product p with i.productId=p.id WHERE p.hasDeleted=0 GROUP BY productId ORDER BY COUNT(i.productId) DESC";
	private static final String HQL_FIND_BY_NEW = "FROM Product p WHERE p.hasDeleted=0 ORDER BY p.addTime DESC";
	private static final String HQL_FIND_BY_HOT_NEW = "FROM IndentItem i JOIN Product p with i.productId=p.id WHERE p.hasDeleted=0 AND p.addTime> :addTime GROUP BY productId ORDER BY COUNT(i.productId) desc";
	private static final String HQL_FIND_ALL_COUNT = "SELECT COUNT(id) FROM Product WHERE hasDeleted= 0";
	private static final String HQL_FIND_LIKE_BY_PRODUCTNAME = "FROM Product p WHERE p.hasDeleted = 0 AND p.productName LIKE :productName";
	private static final String HQL_FIND_BY_CATEGORYID_NEW = "FROM CategoryProduct c JOIN Product p ON c.productId=p.id WHERE p.hasDeleted=0 AND c.categoryId= ：categoryId ORDER BY p.addTime DESC";
	private static final String HQL_FIND_BY_CATEGORYID_HOT = "FROM CategoryProduct c JOIN Product p with c.productId=p.id JOIN IndentItem i with i.productId=p.id WHERE p.hasDeleted=0 AND c.categoryId= :categoryId GROUP BY c.productId ORDER BY COUNT(c.productId) DESC";

	@Resource
	private HibernateConnection<Product> hibernateConnection;

	private void assembleProducts(Iterator iterator, List<Product> products) {
		while (iterator.hasNext()) {
			Object[] objects = (Object[]) iterator.next();
			Product product = null;
			for (Object object : objects) {
				if (object instanceof Product) {
					product = (Product) object;
				}
			}
			if (product != null) {
				products.add(product);
			}
		}
	}

	@Override
	public void save(Product t) {
		hibernateConnection.save(t);
	}

	@Override
	public void delete(Product t) {
		// Do nothing because of it unusual
	}

	@Override
	public int update(Product t) {
		hibernateConnection.update(t);
		return 1;
	}

	@Override
	public List<Product> queryAll() {
		// Do nothing because of it unusual
		return null;
	}

	@Override
	public List<Product> queryByHasDeleted(int hasDeleted) {
		Map<String, Object> params = new HashMap<>();
		params.put("hasDeleted", hasDeleted);
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_HASDELETE, params);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);

		return list;
	}

	@Override
	public List<Product> queryByAddTime(long addTime) {
		Map<String, Object> params = new HashMap<>();
		params.put("addTime", addTime);
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_ADDTIME, params);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);
		return list;
	}

	@Override
	public List<Product> queryByCategoryId(int categoryId) {
		Map<String, Object> params = new HashMap<>();
		params.put("categoryId", categoryId);
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_CATEGORYID, params);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);
		return list;
	}

	@Override
	public List<Product> queryAllLimmitDesc(int first, int max) {
		// TODO:id已是string类型，要对这个字段进行排序已没有什么意义了
		return hibernateConnection.find(HQL_FIND_ALL_DESC, first, max);
	}

	@Override
	public Product queryById(String id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		List<Product> list = hibernateConnection.find(HQL_FIND_BY_ID, params);
		if (CollectionUtils.isEmpty(list))
			return null;
		return list.get(0);
	}

	@Override
	public int queryAllCount() {
		return hibernateConnection.count(HQL_FIND_ALL_COUNT).intValue();
	}

	@Override
	public List<Product> queryLimmitByCategoryId(int categoryId, int first, int max) {
		Map<String, Object> params = new HashMap<>();
		params.put("categoryId", categoryId);
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_CAT_ID, params, first, max);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);

		return list;
	}

	@Override
	public List<Product> queryLimmitByHot(int first, int max) {
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_HOT, first, max);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);

		return list;
	}

	@Override
	public List<Product> queryLimmitByNew(int first, int max) {
		return hibernateConnection.find(HQL_FIND_BY_NEW, first, max);
	}

	@Override
	public List<Product> queryLimmitByHotNew(long addTime, int first, int max) {
		Map<String, Object> params = new HashMap<>();
		params.put("addTime", addTime);
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_HOT_NEW, params, first, max);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);

		return list;
	}

	@Override
	public List<Product> queryLimmitLikeByPruductName(String productName, int first, int max) {
		Map<String, Object> params = new HashMap<>();
		params.put("productName", productName);
		return hibernateConnection.find(HQL_FIND_LIKE_BY_PRODUCTNAME, params, first, max);
	}

	@Override
	public List<Product> queryLimmitByCategoryIdNew(int categoryId, int first, int max) {
		Map<String, Object> params = new HashMap<>();
		params.put("categoryId", categoryId);
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_CATEGORYID_NEW, params, first, max);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);

		return list;
	}

	@Override
	public List<Product> queryLimmitByCategoryIdHot(int categoryId, int first, int max) {
		Map<String, Object> params = new HashMap<>();
		params.put("categoryId", categoryId);
		Iterator iterator = hibernateConnection.findGetIterator(HQL_FIND_BY_CATEGORYID_HOT, params, first, max);
		List<Product> list = new ArrayList<>();
		assembleProducts(iterator, list);

		return list;
	}

}
